<script setup lang="ts">
import { type ButtonProps, message } from "ant-design-vue";
import { noop } from "@/framework/util/warning.ts";
import path from "@/framework/util/path.ts";
import { openExternal } from "@/util/shell.ts";

interface Props extends ButtonProps {
  dir?: string;
  file?: string;
  tip?: string;
}

const { dir, file, tip, ...buttonProps } = defineProps<Props>();

const openFile = async (e: PointerEvent) => {
  if (file) {
    const filePath = dir ? path.join(dir, file) : file;
    if (await native.fs.exists(filePath)) {
      openExternal(filePath).catch(noop);
    } else {
      message.warn(tip || "文件不存在");
    }
  }
};
</script>

<template>
  <a-button v-bind="buttonProps" @click="openFile">
    <slot />
  </a-button>
</template>
